package com.zou.controller;

import com.zou.api.enums.BaseEnum;
import com.zou.api.response.BaseResponse;
import com.zou.exception.GlobalException;
import com.zou.model.entity.User;
import com.zou.server.service.UserService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*
 * @auth zoujt
 * @date 2021/3/17
 * @Description
 */
@Api(value = "用户管理")
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;
    Logger logger = LoggerFactory.getLogger(GlobalException.class);
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public  BaseResponse<User> findAll(){
        BaseResponse baseResponse = new BaseResponse(BaseEnum.SUCCESS);
        try {
            List<User> list = userService.findAll();
            baseResponse.setData(list);
        } catch (Exception e) {
            baseResponse = new BaseResponse(BaseEnum.FAIL);
            logger.error("查询用户信息异常：",e);
        }

        return baseResponse;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@RequestBody User user){
        BaseResponse baseResponse = new BaseResponse(BaseEnum.SUCCESS);

        try {
            int result = userService.addUser(user);
            if (result<=0){
                baseResponse = new BaseResponse(BaseEnum.FAIL);
            }
        } catch (Exception e) {
            baseResponse = new BaseResponse(BaseEnum.FAIL);
            logger.error("添加用户信息异常：",e);
        }
        return baseResponse;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public BaseResponse  delete(Long id ){
        BaseResponse baseResponse = new BaseResponse(BaseEnum.SUCCESS);
        try {
            int result = userService.delete(id);
            if (result<=0){
                baseResponse = new BaseResponse(BaseEnum.FAIL);
            }
        } catch (Exception e) {
            baseResponse = new BaseResponse(BaseEnum.FAIL);
            logger.error("删除用户信息异常：",e);
        }
        return baseResponse;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public  BaseResponse update(@RequestBody User user ){
        BaseResponse baseResponse = new BaseResponse(BaseEnum.SUCCESS);
        try {
            int result = userService.update(user);
            if (result<=0){
                baseResponse = new BaseResponse(BaseEnum.FAIL);
            }
        } catch (Exception e) {
            baseResponse = new BaseResponse(BaseEnum.FAIL);
            logger.error("修改用户信息异常：",e);
        }
        return baseResponse;
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public  BaseResponse findById(Long id){
        BaseResponse baseResponse = new BaseResponse(BaseEnum.SUCCESS);
        try {
            User user = userService.findById(id);
            baseResponse.setData(user);
        } catch (Exception e) {
            baseResponse = new BaseResponse(BaseEnum.FAIL);
            logger.error("根据id查询用户信息异常：",e);
        }
        return baseResponse;
    }


    //mybatis-plus
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public List<User> listAll(){
        List<User> users = userService.listAll();
        return users;
    }
    @RequestMapping(value = "/getOne1", method = RequestMethod.GET)
    public  User getOne1(Long id){
        User user = userService.getOne1(id);
        return user;
    }

}
